#!/bin/bash

# 如果java环境存在则退出
if [ "$(command -v java)" ]; then
    echo "jdk已经存在"
    exit 0
fi

# 解压安装包
tar -zxvf jdk-8u371-linux-x64.tar.gz -C /usr/local
if [ $? -ne 0 ]; then
    echo "安装包解压失败"
    exit 1
fi

# 配置环境变量
echo '# Java environment variables' >> /etc/profile
echo 'export JAVA_HOME=/usr/local/jdk1.8.0_371' >> /etc/profile
echo 'export JRE_HOME=$JAVA_HOME/jre' >> /etc/profile
echo 'export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH' >> /etc/profile
echo 'export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH' >> /etc/profile

# 重新加载配置文件
source /etc/profile

# 检查Java安装信息
echo "检查Java安装信息"
if [ "$(command -v java)" ]; then
    echo "jdk安装成功" 
    java -version
    else
    echo "jdk安装失败"
fi